import React from 'react';
import {Switch} from 'react-router-dom'
import {Route, Redirect} from 'react-router-dom'
import {observer,inject} from 'mobx-react'
// import RouterView from './RouterView'
// 循环递归渲染路由视图
@inject("homeStore")
@observer
class PrivateRoute extends React.Component {
    util = (routerList) => {
        return routerList.map((route, i) => {
            if (route.children) {
                // 判断用户登录状态和页面是否需要登录
                if(this.props.homeStore.user.isLogin===route.meta.require){
                    return (<Route key={route.name} path={route.path} render={() =>
                        <route.component>
                            {this.util(route.children)}
                            <Redirect exact path={route.path} key={route.name} to={route.redirect}/>
                        </route.component>
                    }/>)
                }else {
                    console.log('没有登录')
                    return <Redirect  key={route.name} to='/login'/>
                }
            } else if (route.redirect) {
                return <Redirect exact path={route.path} key={route.name} to={route.redirect}/>
            } else {
                return (<Route key={route.name} path={route.path} component={route.component}/>)
            }
        })
    }
    render() {
        const {routers} = this.props;

        return (
            <Switch>
                {this.util(routers)}
            </Switch>
        )
    }
}

export default PrivateRoute